<?php
require_once("../../../config.inc.php");
require_once(SERVER_FS_ROOT."/app/_inc/sdao/StructuredDataAccessObject.php");
require_once(SERVER_FS_ROOT."/app/_inc/mix.func.php");
require_once(SERVER_FS_ROOT."/app/util/imex/_inc/importExport.func.php");
$objSDAO = new SDAO($aMetaConPar);
$tipoRic = sxREQUEST('tr');
$module = sxREQUEST('mod');
$subModule = sxREQUEST('sub');
$dataArea = sxREQUEST('da');
$subjectArea = sxREQUEST('sa');
if (sxREQUEST('pwd')!=$remoteRespPassword) {
	echo "@zxcv@KO@zxcv@password errata - vedi remoteRespPassword in config.inc.php@zxcv@";
	exit();	
}
$aRisp = array('mod'=>array(),'sub'=>array(),'process_unit'=>array(),'menu_page'=>array(),
	'data_area'=>array(),'subject_area'=>array(),'entity'=>array(),'relationship'=>array());
switch($tipoRic) {
	case 'contact' :
		echo "@zxcv@OK@zxcv@...@zxcv@";
		break;
	case 'pu' :
		loadModule($aRisp);
		loadSubMod($module, $aRisp);
		loadPU($module, $subModule, $aRisp);
		echo "@zxcv@OK@zxcv@".serialize($aRisp)."@zxcv@";
		break;
	case 'sub' :
		loadModule($aRisp);
		loadSubMod($module, $aRisp);
		echo "@zxcv@OK@zxcv@".serialize($aRisp)."@zxcv@";
		break;
	case 'menu' :
		loadModule($aRisp);
		loadMenu($module, $aRisp);
		echo "@zxcv@OK@zxcv@".serialize($aRisp)."@zxcv@";
		break;
	case 'mod' :
		loadModule($aRisp);
		echo "@zxcv@OK@zxcv@".serialize($aRisp)."@zxcv@";
		break;
	case 'da' :
		loadDataArea($aRisp);
		echo "@zxcv@OK@zxcv@".serialize($aRisp)."@zxcv@";
		break;
	case 'sa' :
		loadDataArea($aRisp);
		loadSubjectArea($dataArea, $aRisp);
		echo "@zxcv@OK@zxcv@".serialize($aRisp)."@zxcv@";
		break;
	case 'ent' :
		loadDataArea($aRisp);
		loadSubjectArea($dataArea, $aRisp);
		loadEntity($dataArea, $subjectArea, $aRisp);
		echo "@zxcv@OK@zxcv@".serialize($aRisp)."@zxcv@";
		break;
	case 'rel' :
		loadDataArea($aRisp);
		loadSubjectArea($dataArea, $aRisp);
		loadRelationship($dataArea, $subjectArea, $aRisp);
		echo "@zxcv@OK@zxcv@".serialize($aRisp)."@zxcv@";
		break;
	case 'exp' :
		$aReqExp = unserialize(sxREQUEST('akey'));
		$aExpTab = array();
		foreach($aReqExp as $z1 => $aReq) {
			$msg = loadExpTab($aReq[0], $aReq[1], $aExpTab );
			if ($msg != '') { 
				echo "@zxcv@KO@zxcv@Error in fase di esportreqtype - ".$msg."@zxcv@";
				break; 
			} 
		}
	 	$serTab = serialize($aExpTab);
		$sFileHeader = str_pad("POLARIS-MB ".date("Y-m-d H:i:s")." ".$objSDAO->dbcPar['dbcType']." ".
			$objSDAO->dbcPar['host']." ".$objSDAO->dbcPar['dbName']." ".
			$_SERVER['SERVER_NAME']." ".$_ENV['COMPUTERNAME'],128);	 	
		echo "@zxcv@OK@zxcv@".$sFileHeader.$serTab."@zxcv@";
		break;	
	default :
		echo "@zxcv@KO@zxcv@errore richiesta - tr=".$tipoRic." non previsto@zxcv@";
}
exit();

function loadModule(&$aRisp) {
	global $objSDAO;
	$sSQL = "SELECT m.module, m.description FROM module m ORDER BY m.module ";
	$objSDAO->query($sSQL);
	while($row=$objSDAO->fetch()) {
		$aRisp['mod'][$row['module']] = $row['description'];
	}
}

function loadSubMod($module, &$aRisp) {
	global $objSDAO;
	$sSQL = "SELECT s.sub_module, s.description FROM sub_module s " .
			" WHERE s.module = '".$module."'" .
			" ORDER BY s.functional_area, s.sub_module ";
	$objSDAO->query($sSQL);
	while($row=$objSDAO->fetch()) {
		$aRisp['sub'][$row['sub_module']] = $row['description'];
	}
}

function loadMenu($module, &$aRisp) {
	global $objSDAO;
	$sSQL = "SELECT m.module, m.menu_page, m.description FROM menu_page m " .
			" WHERE m.module = '".$module."'" .
			" ORDER BY m.menu_page ";
	$objSDAO->query($sSQL);
	while($row=$objSDAO->fetch()) {
		$aRisp['menu_page'][$row['module'].'@'.$row['menu_page']] = $row['description'];
	}
}

function loadPU($module, $subModule, &$aRisp) {
	global $objSDAO;
	$sSQL = "SELECT pu.process_unit, pu.description FROM process_unit pu " .
			" WHERE pu.module = '".$module."' ";
	if ($subModule != '*') {
		$sSQL .= " AND pu.sub_module = '".$subModule."' ";
	}
	$sSQL .=	" ORDER BY pu.process_unit ";
	$objSDAO->query($sSQL);
	while($row=$objSDAO->fetch()) {
		$aRisp['process_unit'][$row['process_unit']] = $row['description'];
	}
}

function loadDataArea(&$aRisp) {
	global $objSDAO;
	$sSQL = "SELECT da.data_area, da.description FROM data_area da " .
			" ORDER BY da.data_area ";
	$objSDAO->query($sSQL);
	while($row=$objSDAO->fetch()) {
		$aRisp['data_area'][$row['data_area']] = $row['description'];
	}
}

function loadSubjectArea($dataArea, &$aRisp) {
	global $objSDAO;
	$sSQL = "SELECT sa.data_area, sa.subject_area, sa.description FROM subject_area sa " .
			" WHERE sa.data_area = '".$dataArea."'" .
			" ORDER BY sa.subject_area ";
	$objSDAO->query($sSQL);
	while($row=$objSDAO->fetch()) {
		$aRisp['subject_area'][$row['subject_area']] = $row['description'];
	}
}

function loadEntity($dataArea, $subjectArea, &$aRisp) {
	global $objSDAO;
	$sSQL = "SELECT e.entity, e.description FROM entity e ";
	if ($subjectArea != '*') {
		$sSQL .= " INNER JOIN subject_area_entity sae ON sae.entity = e.entity " .
					"    AND sae.subject_area = '".$subjectArea."' ";
	}
	$sSQL .= " WHERE e.data_area = '".$dataArea."' ORDER BY e.entity ";
	$objSDAO->query($sSQL);
	while($row=$objSDAO->fetch()) {
		$aRisp['entity'][$row['entity']] = $row['description'];
	}
}

function loadRelationship($dataArea, $subjectArea, &$aRisp) {
	global $objSDAO;
	$sSQL = "SELECT r.* FROM relationship r " .
			" INNER JOIN entity e ON r.parent_entity = e.entity ";
	if ($subjectArea != '*') {
		$sSQL .= " INNER JOIN subject_area_entity sae ON sae.entity = e.entity " .
					"    AND sae.subject_area = '".$subjectArea."' ";
	}
	$sSQL .= " WHERE e.data_area = '".$dataArea."'" .
				" ORDER BY r.parent_entity, r.child_entity, r.number ";
	$objSDAO->query($sSQL);
	while($row=$objSDAO->fetch()) {
		$aRisp['relationship'][$row['parent_entity'].'@'.$row['child_entity'].'@'.$row['number']] = 
			$row['description_parent_child'];
	}
}
?>
